<?php
class Users extends CActiveRecord
{
  public $verifyCode;
  const STATUS_INACTIVE = 0;
  const STATUS_PENDING = 1;
  const STATUS_APPROVED = 2;
  const STATUS_REJECT = 3;

  const STATUS_PAID = 1;
  const STATUS_NOT_PAID = 0;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
      array('id, verifyCode, username, name, address, city, school_name, membership_fee, gift_type, gift_value, password_salt, expired_date, payment_confirm', 'safe'),
      array('email', 'required', 'message'=>'Please enter a value for {attribute}.', 'on' => 'program'),
      array('email', 'email', 'message' => 'Please enter a valid email address.', 'on' => 'program'),
      //array('email', 'unique', 'on' => 'program'),
      array('email, username', 'required', 'on' => 'forgot'),
      array('email', 'email', 'message' => 'Please enter a valid email address.', 'on' => 'forgot'),
      array('email', 'required', 'message'=>'Please enter a value for {attribute}.', 'on' => 'create'),
      //array('email', 'unique', 'on' => 'create'),
      array('password', 'length', 'min' => 6, 'on' => 'create'),
			// array('member_id', 'numerical', 'integerOnly'=>true),
			array('name, address', 'length', 'max'=>200),
      array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on' => 'program'),
      array('verifyCode', 'CaptchaExtendedValidator', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on' => 'valid'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, address, member_id', 'safe', 'on'=>'search'),
			array('email, username, password, name, member_id', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'photos' => array(self::HAS_MANY, 'Photos', 'user_id'),
            'orders' => array(self::HAS_MANY, 'Orders', 'user_id'),
            'member' => array(self::BELONGS_TO, 'Members', 'member_id'),
            'program' => array(self::BELONGS_TO, 'Programs', 'program_id'),
            'cciht' => array(self::HAS_ONE, 'Cciht', 'user_id'),
            'accreditation' => array(self::HAS_ONE, 'Accreditation', 'user_id'),
            'school' => array(self::HAS_MANY, 'School', 'user_id'),
            'invoice' => array(self::HAS_ONE, 'Invoices', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'email' => 'Email',
			'username' => 'Username',
			'password' => 'Password',
			'name' => 'Name',
			'address' => 'Address',
			'city' => 'City',
			'school_name' => 'School Name',
			'member_id' => 'Member',
			'program_id' => 'Program',
			'status' => 'Status Approved',
			'membership_fee' => 'Membership Fee',
			'gift_type' => 'Gift Type',
			'gift_value' => 'Gift Value',
			'created_at' => 'Created At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('birthdate',$this->birthdate,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('school_name',$this->school_name,true);
		$criteria->compare('member_id',$this->member_id);
		$criteria->compare('program_id',$this->program_id);
		$criteria->compare('status_approved',$this->status_approved);
		$criteria->compare('membership_fee',$this->membership_fee);
		$criteria->compare('gift_type',$this->gift_type,true);
		$criteria->compare('gift_value',$this->gift_value);
		$criteria->compare('created_at',$this->created_at,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}